<template>
  <div>
    <el-card>
      <CategorySelector
        @changeCategoryId="changeCategoryId"
        :isShowList="isShowList"
      >
      </CategorySelector>
    </el-card>
    <el-card style="margin-top: 20px">
      <div v-show="!isShowSpu && !isShowSku">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addSpu"
          :disabled="!category3Id"
          >添加Spu</el-button
        >
        <el-table :data="spuList" style="width: 100%">
          <el-table-column type="index" label="序号" width="width">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <HintButton
                title="添加sku"
                size="mini"
                type="success"
                icon="el-icon-plus"
                @click="addSku(row)"
              ></HintButton>
              <HintButton
                title="修改spu"
                size="mini"
                type="warning"
                icon="el-icon-edit"
                @click="updateSpu(row)"
              ></HintButton>
              <HintButton
                title="查看spu下的sku列表"
                size="mini"
                type="info"
                icon="el-icon-info"
                @click="lookSku(row)"
              ></HintButton>
              <HintButton
                title="删除spu"
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteSpu(row)"
              ></HintButton>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
          background
          layout=" prev, pager, next, jumper, ->, sizes,total"
          :total="total"
          :current-page="this.page"
          :page-count="7"
          :page-size="limit"
          :page-sizes="[3, 5, 7]"
        >
        </el-pagination>
      </div>
      <SkuForm
        v-show="isShowSku"
        ref="skuForm"
        :visible.sync="isShowSku"
      ></SkuForm>
      <SpuForm
        v-show="isShowSpu"
        ref="spuForm"
        :visible.sync="isShowSpu"
        sku="spuForm"
        @backsuccess="backsuccess"
      ></SpuForm>
    </el-card>
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="handlerClose"
    >
      <el-table :data="skuList"  v-loading="loading">
        <el-table-column
          property="skuName"
          label="名称"
          width="150"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="150"
        ></el-table-column>
        <el-table-column
          property="weight"
          label="重量"
          width="150"
        ></el-table-column>
        <el-table-column
          label="默认图片"
          width="150"
        >
        <template slot-scope="{row,$index}">
          <img :src="row.skuDefaultImg" alt="" width="100px" height="80px">
        </template>
        </el-table-column>
        <el-table-column property="address" label="地址"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from "@/views/product/spu/SkuForm";
import SpuForm from "@/views/product/spu/SpuForm";
export default {
  name: "SpuList",
  data() {
    return {
      isShowList: true,
      isShowSpu: false,
      isShowSku: false,
      flag: 0,
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1,
      limit: 3,
      spuList: [],
      total: 0,
      spu: {},
      skuList: [],
      loading: false,
      dialogTableVisible: false,
    };
  },
  components: {
    SkuForm,
    SpuForm,
  },
  methods: {
    changeCategoryId({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId;
      } else if (level === 2) {
        this.category2Id = categoryId;
      } else {
        this.category3Id = categoryId;
        this.getSpuList();
      }
    },
    handleSizeChange(size) {
      this.limit = size;
      this.getSpuList();
    },
    async getSpuList(page = 1) {
      this.page = page;
      try {
        let result = await this.$API.spu.getList(
          this.page,
          this.limit,
          this.category3Id
        );
        if (result.code === 200 || result.code === 20000) {
          this.spuList = result.data.records;
          this.total = result.data.total;
        }
      } catch (e) {
        this.$message("获取spu列表失败");
      }
    },
    addSpu() {
      this.flag = 0;
      this.isShowSpu = true;
      this.$refs.spuForm.getInitAddFormData(this.category3Id);
    },
    addSku(row) {
      this.isShowSku = true;
      this.$refs.skuForm.getInitSkuData(
        row,
        this.category1Id,
        this.category2Id
      );
    },
    updateSpu(row) {
      this.flag = row.id;
      this.isShowSpu = true;
      this.$refs.spuForm.getInitUpdateFormData(row.id, this.category3Id);
    },
    backsuccess() {
      if (flag) {
        this.getSpuList(this.page);
      } else {
      }
      this.flag = 0;
    },
    async lookSku(row) {
      this.spu = row;
      this.dialogTableVisible = true
      this.loading = true
      let result = await this.$API.sku.getListBySpuId(row.id);
      this.loading = false
     this.skuList = result.data;
    },
    handlerClose() {
      this.skuList = []
       this.dialogTableVisible = false
    },
    deleteSpu(row) {
      this.$confirm(`您确定要删除${row.spuName}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let result = await this.$API.spu.remove(row.id);
          if (result.code === 200 || result.code === 20000) {
            this.$message.success("删除成功");
            this.getSpuList(this.spuList < 1 ? this.page - 1 : this.page);
          }
        })
        .catch(() => {
          this.$message.success("删除失败");
        });
    },
  },
  watch: {
    isShowSku(newVal, oldVal) {
      this.isShowList = !newVal;
    },
    isShowSku(newVal, oldVal) {
      this.isShowList = !newVal;
    },
  },
};
</script>

<style lang="scss">
</style>